<?php
/*
Template Name: Event
*/
?>
<?php get_header(); ?>
<a name="top"></a>
<div id="wrapper">
	<?php if(function_exists("gltr_build_flags_bar")) { gltr_build_flags_bar(); } ?>

	<!-- ヘッダー START -->
	<div id="header">
		<h1><a href="<?php bloginfo('url'); ?>"><img src="<?php bloginfo('template_url'); ?>/images/front/img_h1.png" alt="平山美春  " title="平山美春  " /></a></h1>
	</div>
	<!-- //ヘッダー END// -->



	<!-- グローバルナビSTART -->
	<?php echo get_global_menu(); ?> 
	<!-- //グローバルナビEND// -->

	<!-- コンテンツ START -->
        <div class="info_main clearfix">
          <div id="main" class="clearfix">
            <h2 class="info">Information</h2>
	<!-- //コンテンツタイトルEND// -->

	<!-- パン屑START -->
	<div id="bread">
		<a title="TOP" href="<?php bloginfo('url'); ?>">TOP</a>&nbsp;&gt;&nbsp;&nbsp;Information
	</div>
	<!-- パン屑END -->

	<!-- 検索条件START -->
	<?php if(!is_single()): ?>
	<div id="search_box">
		
		<div class="category">
			<p><img src="<?php bloginfo('template_url'); ?>/images/front/stitle_cat.png"></p>
			<?php echo get_selectbox_category(); ?>
		</div>
		<div class="monthly">
			<p><img src="<?php bloginfo('template_url'); ?>/images/front/stitle_month.png"></p>
			<?php echo get_selectbox_monthly(); ?>
		</div>
		
	</div>
	<?php endif; ?>
	<!-- //検索条件END// -->

	
	<!-- ブログSTART -->

	<!-- ##シングルページへのアクセスだった場合（single.phpからのincludeの場合）は表示させるPostIDを取得 -->
	<?php if(is_single()){ $displayablePostID = get_the_ID();}else{$displayablePostID = "";} ?>

	<!-- ##Eventポスト取得のループ開始 -->
	<?php wp_reset_query();query_posts('post_type=event&meta_key=eventstart&orderby=meta_value&order=ASC'); ?>
	<?php if(have_posts()): ?>
	    <?php while(have_posts()): the_post(); ?>
	    <?php $event = get_event_meta(); ?>
	    <!-- ##イベントの非表示条件を判定  -->
	    <?php if(is_event_disable($event)){continue;} ?>
	    <!-- ##シングルページの場合は、指定されたPostID以外は表示させない  -->
	    <?php if($displayablePostID !== "" && $displayablePostID !== get_the_ID()){ continue; } ?>
	    
            <div class="info_box">
                <div class="info_top">
				<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
				<span class="date"><?php echo $event["startdate"]; ?></span>
			</div>
			<div class="info_body">
				<p><img src="<?php bloginfo('template_url'); ?>/images/front/ico_<?php echo $event["category"]; ?>.png"></p>
				<div class="detail"><?php the_content(); ?></div>
			</div>
		</div>
		<div class="info_bottom">
				<?php echo getLikeBottun(); ?>
		</div>
		<!-- //インフォ詳細END// -->

		
	    <?php endwhile; ?>
	<?php endif; ?>
	<!-- //ブログEND// -->

	<!-- Pagetop START -->
	<div class="pagetop">
		<p><a href="#">▲Page Topに戻る</a></p>
	</div>
	<!-- //Pagetop END// -->
	
	<!-- フッター START -->
	<div id="footer">
		<p>Copyright&copy; LesPros Entertainment Co., Ltd. All rights reserved.</p>
	</div>
	<!-- //フッター END// -->
</div>

<?php 

function get_event_meta(){
    $c = get_post_custom_values('eventstart', get_the_ID()); $event["startdate"] = $c[0];
    $c = get_post_custom_values('eventend'  , get_the_ID()); $event["enddate"]   = $c[0];
    $c = get_post_custom_values('opendate'  , get_the_ID()); $event["opendate"]  = $c[0];
    $c = get_post_custom_values('closedate' , get_the_ID()); $event["closedate"] = $c[0];
    $c = get_post_custom_values('category'  , get_the_ID()); $event["category"]  = $c[0];
    if($event["enddate"] == "" || $event["enddate"] === $event["startdate"]){$event["datelabel"] = $event["startdate"];}
    if($event["enddate"] != "" && $event["enddate"] != $event["startdate"]){$event["datelabel"] = $event["startdate"] . " - " . $event["enddate"];}
    return $event;
}

function is_event_disable($event){
    
    $filtered = false;
    
    // ##opendate <- -> closedate 以外であれば非表示
    if( $event["opendate"] != ""){ if(strtotime( $event["opendate"] ) > time() ){ $filtered = true; }}
    if( $event["closedate"] != ""){ if(strtotime( $event["closedate"] ) < time() ){ $filtered = true; }}

    //##月指定があれば絞り込み
    if(isset($_REQUEST["mon"])){
	$filter = date("Y", $_REQUEST["mon"]) . date("m", $_REQUEST["mon"]);
	$current = date("Y", strtotime($event["startdate"])) . date("m", strtotime($event["startdate"]));
	if($filter != $current){$filtered = true;}
    }

    //##カテゴリ指定があれば絞り込み
    if(isset($_REQUEST["cat"])){
	if($_REQUEST["cat"] != $event["category"]){$filtered = true;}
    }
    return $filtered;
    
}

function get_selectbox_category(){
    wp_reset_query();
    query_posts('post_type=event');
    if(have_posts()){
	while(have_posts()){
	    the_post();
	    $e = get_event_meta();
	    if( $e["opendate"] != ""){ if(strtotime( $e["opendate"] ) > time() ){ continue; }}
	    if( $e["closedate"] != ""){ if(strtotime( $e["closedate"] ) < time() ){ continue; }}

	    $cat = get_post_custom_values('category', get_the_ID());
	    
	    $catList[$e["category"]] = "";
	}
    }
    $catListKeys = array_keys($catList);

    $selectbox_html  = "<select name='cat' id='cat' class='postform' >";
    $selectbox_html .= "<option value=''>カテゴリーを選択</option>";
    foreach($catListKeys as $k=>$v){
	if($_REQUEST["cat"] === $v){$selected = "selected";}else{$selected = "";}
	$selectbox_html .= '<option class="level-0" value="' . $v . '" ' . $selected . '>' . $v . '</option>';
    }
    $selectbox_html .= "</select>";
    $selectbox_html .= "<script type='text/javascript'>";
    $selectbox_html .= "/* <![CDATA[ */";
    $selectbox_html .= "var catdropdown = document.getElementById('cat');";
    $selectbox_html .= "    function onCatChange() {";
    $selectbox_html .= "	location.href = './?cat='+catdropdown.options[catdropdown.selectedIndex].value;";
    $selectbox_html .= "}";
    $selectbox_html .= "catdropdown.onchange = onCatChange;";
    $selectbox_html .= "/* ]]> */";
    $selectbox_html .= "</script>";
    
    return $selectbox_html;	
}

function get_selectbox_monthly(){
    wp_reset_query();
    query_posts('post_type=event');
    if(have_posts()){
	while(have_posts()){
	    the_post();
	    $e = get_event_meta();
	    if( $e["opendate"] != ""){ if(strtotime( $e["opendate"] ) > time() ){ continue; }}
	    if( $e["closedate"] != ""){ if(strtotime( $e["closedate"] ) < time() ){ continue; }}
	    $monthList[date("Y", strtotime($e["startdate"])) . "/" .  date("n", strtotime($e["startdate"]))] = "";
	}
    }
    $monthListKeys = array_keys($monthList);

    $selectbox_html  = "<select name='mon' id='mon' class='postform' >";
    $selectbox_html .= "<option value=''>月を選択</option>";
    foreach($monthListKeys as $k=>$v){
	if($_REQUEST["mon"] === strtotime($v."/1")){$selected = "selected";}else{$selected = "";}
	$selectbox_html .= '<option class="level-0" value="' . strtotime($v."/1") . '" ' . $selected . '>' . $v . '</option>';
    }
    $selectbox_html .= "</select>";
    $selectbox_html .= "<script type='text/javascript'>";
    $selectbox_html .= "/* <![CDATA[ */";
    $selectbox_html .= "var mondropdown = document.getElementById('mon');";
    $selectbox_html .= "function onMonChange() {";
    $selectbox_html .= "    location.href = './?mon='+mondropdown.options[mondropdown.selectedIndex].value;";
    $selectbox_html .= "}";
    $selectbox_html .= "mondropdown.onchange = onMonChange;";
    $selectbox_html .= "/* ]]> */";
    $selectbox_html .= "</script>";
    
    return $selectbox_html;
}

?>

<?php get_footer(); ?>
